/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.cards;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.internal.CardThumbnail;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.configuration.DeviceCapability;

/**
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class SuggestedCard extends Card {

    public SuggestedCard(Context context) {
        this(context, ResourceTable.Layout_carddemo_suggested_inner_content);
    }

    public SuggestedCard(Context context, int innerLayout) {
        super(context, innerLayout);
        init();
    }

    private void init() {

        //Add a header
        SuggestedCardHeader header = new SuggestedCardHeader(getContext());
        addCardHeader(header);

        //Set click listener
        setClickedListener((card, view) -> new ToastDialog(getContext()).setText("Click listener").show());

        //Set swipe on
        setSwipeable(true);

        //Add thumbnail
        CardThumbnail thumb = new SuggestedCardThumb(getContext());
        thumb.setUrlResource("https://lh5.googleusercontent.com/-N8bz9q4Kz0I/AAAAAAAAAAI/AAAAAAAAAAs/Icl2bQMyK7c/s265-c-k-no/photo.jpg");
        thumb.setErrorResource(ResourceTable.Media_ic_error_loadingorangesmall);
        addCardThumbnail(thumb);
    }

    @Override
    public void setupInnerViewElements(ComponentContainer parent, Component view) {

        if (view != null) {
            Text title = (Text) view.findComponentById(ResourceTable.Id_carddemo_suggested_title);
            Text member = (Text) view.findComponentById(ResourceTable.Id_carddemo_suggested_memeber);
            Text subtitle = (Text) view.findComponentById(ResourceTable.Id_carddemo_suggested_subtitle);
            Text community = (Text) view.findComponentById(ResourceTable.Id_carddemo_suggested_community);

            if (title != null)
                title.setText(ResourceTable.String_demo_suggested_title);

            if (member != null)
                member.setText(ResourceTable.String_demo_suggested_member);

            if (subtitle != null)
                subtitle.setText(ResourceTable.String_demo_suggested_subtitle);

            if (community != null)
                community.setText(ResourceTable.String_demo_suggested_community);
        }
    }


}

class SuggestedCardHeader extends CardHeader {

    public SuggestedCardHeader(Context context) {
        this(context, ResourceTable.Layout_carddemo_suggested_header_inner);
    }

    public SuggestedCardHeader(Context context, int innerLayout) {
        super(context, innerLayout);
    }

    @Override
    public void setupInnerViewElements(ComponentContainer parent, Component view) {

        if (view != null) {
            Text Text = (Text) view.findComponentById(ResourceTable.Id_text_suggested_card1);

            if (Text != null) {
                Text.setText(ResourceTable.String_demo_suggested_header);
            }
        }
    }
}

class SuggestedCardThumb extends CardThumbnail {

    public SuggestedCardThumb(Context context) {
        super(context);
    }

    @Override
    public void setupInnerViewElements(ComponentContainer parent, Component viewImage) {
        if (viewImage != null) {

            if (parent != null && parent.getResourceManager() != null) {
                DeviceCapability metrics = parent.getResourceManager().getDeviceCapability();

                int base = 100;

                if (metrics != null) {
                    viewImage.getLayoutConfig().width = (int) (base * metrics.screenDensity);
                    viewImage.getLayoutConfig().height = (int) (base * metrics.screenDensity);
                } else {
                    viewImage.getLayoutConfig().width = 200;
                    viewImage.getLayoutConfig().height = 200;
                }
            }
        }
    }
}